package com.donghua.handler;

import com.donghua.entity.ErrorInfo;
import com.donghua.exception.BizException;
import com.donghua.util.SensitiveFilter;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @author Cai Lideng
 * @date 2022年04月20日 上午11:01
 * @description 全局捕获异常返回给前端
 */

@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    @ExceptionHandler({BizException.class})
    @ResponseBody
    public ErrorInfo handleBizException(BizException e){
        this.logger.info("------------全局捕获BIZ异常开始------------");
        ErrorInfo r = new ErrorInfo();
        r.setCode(e.getCode());
        r.setMessage(e.getMessage());
        this.logger.info("ErrorInfo==" + ToStringBuilder.reflectionToString(r));
        this.logger.info("------------全局捕获BIZ异常结束------------");
        return r;
    }

}
